trigger UpdateDispositionInfo on Notes__c (after insert) 
{
List<Notes__c> notesObj = new List<Notes__c>();
List<Lead> ldlist = new List<Lead>();
List<Account> acclist = new List<Account>();
List<Opportunity> opplist = new List<Opportunity>();
List<Id> leadId = new List<Id>();
List<Id> oppId = new List<Id>();
List<Id> accId = new List<Id>();
List<Lead> UpdateLead = new List<Lead>();
List<Account> UpdateAcc= new List<Account>();
List<Opportunity> UpdateOpp= new List<Opportunity>();

try
{
    for(Notes__c newnotes:Trigger.new)
    {

        accId.add(newnotes.Account__c);
        leadId.add(newnotes.lead__c);
        oppId.add(newnotes.Opportunity__c);
        notesObj.add(newnotes);
        System.debug('List things'+notesObj);

    }

    if(leadId.size()>0){    
        ldlist =[Select id,disposition__c,Bad_Reason__c,Disposition_Reason__c,Last_Worked__c,Follow_Up_Date__c from Lead where Id IN:leadId];
            for(lead updateld:ldlist){
                for(Notes__c newnotes1:Trigger.new){
                    if(newnotes1.Lead__c==updateld.Id){
                        updateld.disposition__c=newnotes1.Disposition__c;
                        updateld.Disposition_Reason__c=newnotes1.Disposition_Reason__c;
                        updateld.Last_Worked__c=newnotes1.CreatedDate;
                        updateld.Bad_Reason__c=newnotes1.Disposition_Reason__c;
                        updateld.Follow_Up_Date__c=newnotes1.Follow_Up__c;
                            if(newnotes1.Disposition_Reason__c == 'Do Not Call'){
                                updateld.DoNotCall  = True;
                            }
                        UpdateLead.add(updateld);
                    }
                }
            }
        if(UpdateLead.size()>0){
            update UpdateLead;
        }
    }
    
// update account 
    if(accId.size()>0){
    	
    	  // updated by plo, 06/11/12, AD-31913
    	  Map<Id,RecordType> rtCRSNotesMap = New Map<Id,RecordType>([SELECT Id FROM RecordType WHERE sObjectType = 'Notes__c' AND Name IN :GeneralConstants.RT_CRS_NOTES]);   	  
    
        acclist =[Select Id,Last_Disposition__c,Last_Worked__c,Follow_Up__c from Account where Id IN:accId];
            for(Account updateaccld:acclist){
                for(Notes__c newnotes2:Trigger.new){
                    if(newnotes2.Account__c==updateaccld.Id){
                    	   /* 
                    	      updated by plo, 06/11/12, AD-31913
                	         to exclude account last disposition and last worked on date from 
                	         logging disposition for CRS notes
                	      */
                    	   if(!rtCRSNotesMap.containskey(newnotes2.RecordTypeId))
                    	   {
                           updateaccld.Last_Disposition__c=newnotes2.Account_Disposition__c;
                           updateaccld.Last_Worked__c=newnotes2.CreatedDate;
                           updateaccld.Follow_Up__c=newnotes2.Follow_Up__c;
                    	   }
                        UpdateAcc.add(updateaccld);
                    }
                }
            }
        if(UpdateAcc.size()>0){
            update UpdateAcc;
        }
    }
//BM: Opportunity Disposition here
    if(oppId.size()>0){
        opplist =[Select Id,Disposition__c,Follow_Up__c from Opportunity where Id IN:oppId];
            for(Opportunity updateoppld:opplist){
                for(Notes__c newnotes2:Trigger.new){
                    if(newnotes2.Opportunity__c==updateoppld.Id){
                        updateoppld.Disposition__c=newnotes2.Opportunity_Disposition__c;
                        updateoppld.Follow_Up__c=newnotes2.Follow_Up__c;
                        UpdateOpp.add(updateoppld);
                    }
                }
            }
        if(UpdateOpp.size()>0){
            update UpdateOpp;
        }
    }

}
catch(Exception e){
    system.debug('**** exception'+e);
}
}